package Testing.Projektarbete;


import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import java.net.*;
import java.io.*;

public class TestClient extends JFrame{
    
	private static final long serialVersionUID = 1L;
	private JButton connect;
	private JButton end;
	private JButton login = new JButton("login");
    private JTextArea textarea_received_message;
    private DataInputStream in;
    private DataOutputStream out;
    private JTextField userName = new JTextField(15);
    private JTextField password = new JTextField(15);
    private JScrollPane jsp;
    private Socket socket;

    
    public TestClient() {
    	
        class Receive extends Thread{   
            public void run(){
                try{
                    while(true){
                        textarea_received_message.append(in.readUTF()+"\n");
                        textarea_received_message.setCaretPosition(textarea_received_message.getDocument().getLength());
                	}
                }catch(IOException e){
                }
            }
        }
        
        class MessageWindow extends JPanel{
			private static final long serialVersionUID = 1L;
			
            public MessageWindow(){
                add(new JLabel("UserName: "));
                add(userName);
                add(new JLabel("Password: "));
                add(password);
                add(login);
            }
        }
        
        textarea_received_message=new JTextArea();
        jsp=new JScrollPane(textarea_received_message);

        textarea_received_message.setEditable(false);
        
        connect=new JButton("Connect");
        end=new JButton("End");
             
        connect.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                try{
                	socket=new Socket(InetAddress.getLocalHost(),5000);

                	in=new DataInputStream(socket.getInputStream());
                	out=new DataOutputStream(socket.getOutputStream());                      	
                    new Receive().start();
                    connect.setVisible(false);

                    add(end,BorderLayout.SOUTH);            
                }catch(IOException ioe){
                    ioe.printStackTrace();
                }
            }                
        });
        
        end.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(1);
            }                
        });
        
        login.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                try{
                	out.writeUTF("check");
                	out.writeUTF(userName.getText());  
                    out.writeUTF(password.getText());           
                }catch(IOException ioe){
                    ioe.printStackTrace();
                }
            }                
        });
        
      
        add(new MessageWindow(),BorderLayout.NORTH);
        add(jsp,BorderLayout.CENTER);
        add(connect,BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter(){
        	public void windowClosing(WindowEvent e){
        		System.exit(1);
        	}
        });
        
        setTitle("TestLogin");
        setLocation(200,200);
        setSize(800,500);
        setVisible(true);      
    }
    
    public static void main (String[] args) {
        new TestClient();
    }
}
